package com.smart.web;

import com.smart.dao.GoodsDao;
import com.smart.domain.goods;
import com.smart.service.CartService;
import com.smart.service.GoodsService;
import com.sun.javafx.scene.control.skin.LabeledImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import net.sf.json.util.JSONStringer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Jack on 2015/1/7.
 */
@Controller
public class GoodsQueryController {
    @Autowired
    private GoodsService service;

    @RequestMapping("list")
    public  String listPage(HttpServletRequest request){
        request.setAttribute("page",service.getPage());
        System.out.println(service.getType());

        return "/main_index";
    }

    @RequestMapping(value="goods_query",produces = "text/html;charset=UTF-8")
        @ResponseBody
        public String getUserById(HttpServletRequest request,HttpServletResponse response) {
        response.setContentType("text/json; charset=UTF-8");
        int page = Integer.valueOf(request.getParameter("page"));
        List list =service.getGoodsList(page);
        System.out.println(request.getParameter("page"));
        String array = JSONArray.fromObject(list).toString();

        return  array;
    }


    @RequestMapping(value = "type_query",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getType(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/json; charset=UTF-8");
        return JSONArray.fromObject(service.getType()).toString();

    }



    @RequestMapping(value="goods_type_query",produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String getUserByType(HttpServletRequest request,HttpServletResponse response,String typename) throws  Exception{
        String typekey=URLDecoder.decode(typename,"UTF-8");
        String array = JSONArray.fromObject(service.getGoodsListByType(typekey)).toString();
        return  array;
    }
}
